// api/log_api/log_read.go
package log_api

import (
	"github.com/gin-gonic/gin"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

func (LogApi) LogReadView(c *gin.Context) {
	var cr models.IDRequest
	err := c.ShouldBindUri(&cr)
	if err != nil {
		res.FailWithError(c, err)
		return
	}
	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)

	query := global.DB.Where("")
	if claims.IsAuth() {
		query.Where("user_id = ?", claims.UserID)
	}

	var log models.LogModel
	err = global.DB.Where(query).Take(&log, cr.ID).Error
	if err != nil {
		res.FailWithMsg(c, "日志不存在")
		return
	}
	if !log.IsRead {
		global.DB.Model(&log).Update("is_read", true)
	}
	res.OkWithMsg(c, "日志读取成功")
}
